package com.zhuss.product.processor;

import java.util.Date;

import com.zhuss.exception.ExceptionCode;
import com.zhuss.product.db.dao.ext.PrelineSignDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.preline.DeletePrelineBo;
import com.zhuss.bean.vo.product.preline.DetailPrelineVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.product.db.dao.ext.PrelineDao;
import com.zhuss.product.db.entity.Preline;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.util.JsonUtil;

/**
 * @description: 招募删除
 * @author: mkh
 * @create: Mon Jan 04 13:38:02 CST 2021
 **/
@Component("prelineDelete")
public class PrelineDelete extends AbstractProcessor<Object, DeletePrelineBo> implements Processor<Object, DeletePrelineBo> {

    @Autowired
    private PrelineDao prelineDao;

    @Autowired
	@Qualifier("prelineRead")
	private PrelineRead prelineRead;

	@Autowired
	private PrelineSignDao prelineSignDao;


    /**
     * 校验
     */
	@Override
    public void check(DeletePrelineBo bo) throws DzcxException {
		// TODO 删除 招募 校验， 请补充
		if (bo.getPrelineId() == null) {
			throw new DzcxException(ExceptionCode.请选择数据);
		}

		//线路有报名不让删除
		if (prelineSignDao.countByPrelineId(bo.getPrelineId()) > 0) {
			throw new DzcxException(ExceptionCode.已有报名信息);
		}
    }

    /**
     * 操作方法
     */
    @Override
    public Object process(DeletePrelineBo param) throws DzcxException {
    	Long prelineId = param.getPrelineId();
    	String name = ((DetailPrelineVo) prelineRead.detail(prelineId)).getName();

    	Preline record = new Preline();
    	record.setPrelineId(prelineId);
    	record.setDeleted(param.getDeleted());

    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	prelineDao.delete(record);
    	param.setName(name);
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(DeletePrelineBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.商品.key());
    	record.setAction(LogActionEnum.招募新线删除.key());
    	record.setTargetId(param.getPrelineId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
